package com.baidu.logindemos.ui.main

import android.content.Intent
import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.inputmethod.InputMethodManager
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.widget.addTextChangedListener
import androidx.viewbinding.ViewBinding
import com.baidu.logindemos.R
import com.baidu.logindemos.databinding.ActivityMainBinding
import com.baidu.logindemos.databinding.ActivityWelcomeBinding
import com.baidu.logindemos.db.model.SharedPreferencesUtil
import com.baidu.logindemos.db.model.UserModel
import com.baidu.logindemos.ui.base.BaseActivity
import com.baidu.logindemos.ui.welcome.WelcomeActivity
import com.baidu.logindemos.utils.DialogUtil
import com.baidu.logindemos.utils.KeyBoardUtil
import java.nio.file.WatchEvent
import java.security.Key

/**
 * 登录的主界面
 */
class MainActivity : BaseActivity() {
    private lateinit var mBinding: ActivityMainBinding
    override fun initViewBinding(): ViewBinding {
        return ActivityMainBinding.inflate(layoutInflater)
    }

    override fun initUI() {
        mBinding = binding as ActivityMainBinding

        //切换到欢迎页
        showWelcome()

        //监听EditText事件
        mBinding.etName.addTextChangedListener(afterTextChanged = {
            checkBtnStatus()
        })
        mBinding.etPassword.addTextChangedListener(onTextChanged = { text, start, before, count ->

        },afterTextChanged = {
            checkBtnStatus()
        })

        //登录按钮点击事件
        mBinding.loginBtn.setOnClickListener {
            val name = mBinding.etName.text.toString()
            val user = SharedPreferencesUtil.instance.findUser(name)
            if (user == null){
                showError("用户不存在")
            }else{
                if (user.password == mBinding.etPassword.text.toString()){
                    showSuccess()
                }else{
                    showError("密码错误")
                }
            }
        }

        //密码显示与否按钮事件
        mBinding.ivEye.setOnClickListener {
            val tag = mBinding.ivEye.tag as String
            if (tag == "0"){
                mBinding.etPassword.transformationMethod = HideReturnsTransformationMethod()
                mBinding.ivEye.tag = "1"
            }else{
                mBinding.etPassword.transformationMethod = PasswordTransformationMethod()
                mBinding.ivEye.tag = "0"
            }
            mBinding.etPassword.setSelection(mBinding.etPassword.text.length)

        }
    }

    private fun showError(msg:String){
        DialogUtil.instance.showError(this,msg){

//            val user = UserModel(mBinding.etName.text.toString(),mBinding.etPassword.text.toString())
//            SharedPreferencesUtil.instance.saveUser(user)

            mBinding.etName.text.clear()
            mBinding.etPassword.text.clear()
            mBinding.etName.requestFocus()
            KeyBoardUtil.instance.show(mBinding.etName)


        }
    }
    private fun showSuccess(){
        DialogUtil.instance.showOK(this){
            //跳转界面
        }
    }

    private fun checkBtnStatus(){
        mBinding.loginBtn.isEnabled = mBinding.etName.text.isNotEmpty() && mBinding.etPassword.text.isNotEmpty()
    }

    //跳转到欢迎界面
    private fun showWelcome(){
        startActivity(Intent(this, WelcomeActivity::class.java))
    }
}